<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
	xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
	xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
	xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
	xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">
	<flow name="OrderProcessingFlow1" doc:name="OrderProcessingFlow1">
		<jms:inbound-endpoint queue="WGRUS.INCOMMING_ORDER"
			connector-ref="Active_MQ" doc:name="JMS" />
		<!-- <enricher doc:name="Message Enricher" target="#[xpath('//OrderDetails/OrdreId/text()').text]" 
			> -->
		<enricher doc:name="Message Enricher" target="#[variable:orderId]">
			<flow-ref name="getNextOrderId" doc:name="getNextOrderId" />
		</enricher>
		<mulexml:dom-to-xml-transformer
			doc:name="DOM to XML" />
		<mulexml:xslt-transformer
			maxIdleTransformers="2" maxActiveTransformers="5" doc:name="XSLT"
			xsl-file="mappings/OrderFillOrderNr.xsl" doc:description="Add Order to order document">
			<mulexml:context-property key="orderId"
				value="#[flowVars['orderId']]" />
		</mulexml:xslt-transformer>
		<mulexml:dom-to-xml-transformer
			doc:name="DOM to XML" />
		<set-session-variable variableName="#['order']"
			value="#[payload]" doc:name="Session Variable" />
		<set-session-variable variableName="#['MULE_CORRELATION_SEQUENCE']"
			value="#[flowVars['orderId']]" doc:name="Set Correlation Sequence" />
		<set-session-variable variableName="#['MULE_CORRELATION_GROUP_SIZE']"
			value="#[xpath('count(//OrderLine)')]" doc:name="Set Correlation Group Size" />
		<scripting:transformer doc:name="Script">
			<scripting:script engine="Groovy"><![CDATA[Integer count = (Integer)message.getProperty('MULE_CORRELATION_GROUP_SIZE',org.mule.api.transport.PropertyScope.SESSION);
count = count + 1;
message.setProperty('MULE_CORRELATION_GROUP_SIZE', count, org.mule.api.transport.PropertyScope.SESSION);
result = message;

]]></scripting:script>
		</scripting:transformer>
		<all doc:name="All">
			<flow-ref name="itemStatus" doc:name="Item Status" />
			<flow-ref name="validateStanding" doc:name="Validate Standing" />
		</all>
		<!-- <expression-filter expression="" doc:name="Expression"/> -->
		<!-- <processor-chain doc:name="Processor Chain - Ship order"> <cxf:jaxws-client 
			doc:name="SOAP" operation="OrderShipped" serviceClass="dk.eal.kbr.si.wgrus.accounting.AccountingServices" 
			/> <http:outbound-endpoint exchange-pattern="request-response" host="localhost" 
			port="8081" method="POST" doc:name="HTTP" /> </processor-chain> -->
	</flow>
	<sub-flow name="getNextOrderId" doc:name="getNextOrderId"
		doc:description="Returns the next order id form the Accounting">
		<description>
			Returns the next order id form the Accounting
		</description>

		<processor-chain doc:name="Processor Chain">
			<cxf:jaxws-client doc:name="SOAP" operation="GetNextOrderNumber"
				port="BasicHttpBinding_AccountingServices" serviceClass="dk.eal.kbr.si.wgrus.accounting.AccountingServices" />
			<http:outbound-endpoint exchange-pattern="request-response"
				host="localhost" port="30000" method="POST" doc:name="HTTP"
				path="AccountingServices" />
		</processor-chain>
	</sub-flow>
	<sub-flow name="validateStanding" doc:name="validateStanding">

		<processor-chain doc:name="Processor Chain">
			<cxf:jaxws-client doc:name="SOAP" operation="IsStandingValid"
				serviceClass="dk.eal.kbr.si.wgrus.accounting.AccountingServices" />
			<http:outbound-endpoint exchange-pattern="request-response"
				host="localhost" port="30000" method="POST" doc:name="HTTP"
				path="AccountingServices" />
		</processor-chain>
		<set-payload value="#[(payload.isStandingValid).toString()]"
			doc:name="Set Payload" />
		<set-property value="#[sessionVars['MULE_CORRELATION_GROUP_SIZE']]"
			doc:name="Variable" propertyName="#['MULE_CORRELATION_GROUP_SIZE']" />
		<set-property value="#[sessionVars['MULE_CORRELATION_GROUP_SIZE']]"
			doc:name="Variable" propertyName="#['MULE_CORRELATION_SEQUENCE']" />
		<vm:outbound-endpoint exchange-pattern="one-way"
			path="InventoryResponse" doc:name="VM" />

	</sub-flow>

	<sub-flow name="itemStatus" doc:name="itemStatus">
		<splitter expression="#[xpath('//OrderLine')]" doc:name="Splitter" />
		<mulexml:dom-to-xml-transformer
			doc:name="DOM to XML" />
		<set-property value="#[sessionVars['MULE_CORRELATION_GROUP_SIZE']]"
			doc:name="Variable" propertyName="#['MULE_CORRELATION_GROUP_SIZE']" />
		<choice doc:name="Choice">
			<when expression="#[xpath('//VareId').text.startsWith('G')]">
				<flow-ref name="gadgetItemStatus" doc:name="Gadget Item Status Check" />
			</when>
			<when expression="#[xpath('//VareId').text.startsWith('W')]">
				<flow-ref name="widgetItemStatus" doc:name="Widget Item Status Check" />
			</when>
			<otherwise>
				<logger level="INFO" doc:name="Logger" />
			</otherwise>
		</choice>
		<!-- <flow-ref name="checkOrderJoin" doc:name="Check Order Join" /> -->
	</sub-flow>



	<sub-flow name="gadgetItemStatus" doc:name="gadgetItemStatus">
		<mulexml:xml-to-dom-transformer
			doc:name="XML to DOM" />
		<mulexml:xslt-transformer
			maxIdleTransformers="2" maxActiveTransformers="5" doc:name="XSLT"
			outputEncoding="UTF-8" xsl-file="mappings/OrderItemToGadgetRequest.xsl" />
		<mulexml:dom-to-xml-transformer
			doc:name="DOM to XML" />
		<!-- http://ricston.com/blog/usecase-explaining-behaviour-requestreply-block/ -->

		<jms:outbound-endpoint connector-ref="Active_MQ"
			queue="GADGET.INVENTORY.REQUEST" doc:name="JMS" />
	</sub-flow>

	<sub-flow name="widgetItemStatus" doc:name="widgetItemStatus">
		<mulexml:xml-to-dom-transformer
			doc:name="XML to DOM" />
		<mulexml:xslt-transformer
			maxIdleTransformers="2" maxActiveTransformers="5" doc:name="XSLT"
			outputEncoding="UTF-8" xsl-file="mappings/OrderItemToWidgetRequest.xsl" />
		<mulexml:dom-to-xml-transformer
			doc:name="DOM to XML" />

		<jms:outbound-endpoint connector-ref="Active_MQ"
			queue="WIDGET.INVENTORY.REQUEST" doc:name="JMS" />
	</sub-flow>

	<flow name="gadgetInventoryResponse" doc:name="gadgetInventoryResponse">

		<jms:inbound-endpoint connector-ref="Active_MQ"
			queue="GADGET.INVENTORY.RESPONSE" doc:name="JMS" />
		<expression-transformer doc:name="Expression"
			expression="#[xpath('//PaaLager').text]" />
		<vm:outbound-endpoint exchange-pattern="one-way"
			path="InventoryResponse" doc:name="VM" />
	</flow>

	<flow name="widgetInventoryResponse" doc:name="widgetInventoryResponse">

		<jms:inbound-endpoint connector-ref="Active_MQ"
			queue="WIDGET.INVENTORY.RESPONSE" doc:name="JMS" />
		<expression-transformer doc:name="Expression"
			expression="#[xpath('//InStock').text]" />
		<vm:outbound-endpoint exchange-pattern="one-way"
			path="InventoryResponse" doc:name="VM" />
	</flow>

	<flow name="checkOrderJoin" doc:name="checkOrderJoin">

		<vm:inbound-endpoint exchange-pattern="one-way"
			path="InventoryResponse" doc:name="VM" />

		<collection-aggregator timeout="15000"
			failOnTimeout="true" doc:name="Collection Aggregator" />
		<set-payload
			value="#[for (item : payload) {if(item==&quot;false&quot;) return false;} return true;]"
			doc:name="Set Payload" />
		<logger level="INFO" doc:name="Logger" />
		<vm:outbound-endpoint exchange-pattern="one-way"
			path="OrderStatus" doc:name="VM" />
	</flow>
	<flow name="shipOrder" doc:name="shipOrder">
		<vm:inbound-endpoint exchange-pattern="one-way"
			 doc:name="VM" path="OrderStatus"/>
		<choice doc:name="Choice">
		<when expression="#[payload==true]">
			<processor-chain doc:name="Processor Chain - Ship order">
					<set-payload
			value="#[sessionVars['order']]"
			doc:name="Set Payload" />
				<cxf:jaxws-client doc:name="SOAP" operation="OrderShipped"
					serviceClass="dk.eal.kbr.si.wgrus.accounting.AccountingServices" />
				<http:outbound-endpoint exchange-pattern="request-response"
					host="localhost" port="30000" method="POST" doc:name="HTTP"
					path="AccountingServices" />		
			</processor-chain>
		</when>
            <otherwise>
		<processor-chain doc:name="Processor Chain - Order status invalid">
							<set-payload
			value="#[sessionVars['order']]"
			doc:name="Set Payload" />
                    <jms:outbound-endpoint queue="WGRUS.INVALID_ORDER" connector-ref="Active_MQ" doc:name="JMS"/>
		</processor-chain>
            </otherwise>

		</choice>
	</flow>

</mule>
